package com.ruoyi.audit.controller;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.service.AuditService;
import com.ruoyi.audit.service.IInventoryViewService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.company.domain.Company;
import com.ruoyi.company.domain.CompanyInvoice;
import com.ruoyi.company.domain.CompanyOther;
import com.ruoyi.company.service.ICompanyInvoiceService;
import com.ruoyi.company.service.ICompanyOtherServer;
import com.ruoyi.company.service.ICompanyServer;
import com.ruoyi.manufacturer.service.DjManufacturerService;
import com.ruoyi.mateProcurement.domain.MateProcurement;
import com.ruoyi.mateProcurement.domain.MateProcurementModel;
import com.ruoyi.mateProcurement.mapper.MateProcurementMapper;
import com.ruoyi.material.domain.MateImport;
import com.ruoyi.material.domain.MateImportModel;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.mapper.MateImportMapper;
import com.ruoyi.procurement.domain.ProcurementModel;
import com.ruoyi.procurement.domain.ProcurementOrder;
import com.ruoyi.procurement.service.ProcurementServer;
import com.ruoyi.purchaseReturn.service.IPurchaseReturnService;
import com.ruoyi.sale.service.ISaleGoodsService;
import com.ruoyi.saleReturn.service.ISaleReturnService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 入库审核Controller
 *
 * @author zjh
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/inventoryView")
public class inventoryViewController extends BaseController {

    @Autowired
    MateProcurementMapper mateProcurementMapper;

    private String prefix = "inventoryView";

    @Autowired
    private ProcurementServer procurementServer;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private DjManufacturerService manufacturerService;

    @Autowired
    private ICompanyInvoiceService companyInvoiceService;

    @Autowired
    private ICompanyOtherServer iCompanyOtherServer;

    @Autowired
    private ICompanyServer iCompanyServer;

    @Autowired
    private AuditService auditService;

    @Autowired
    private IInventoryViewService inventoryViewService;

    @Autowired
    private ISaleReturnService iSaleReturnService;

    @Autowired
    private IPurchaseReturnService iPurchaseReturnService;

    @Autowired
    private ISaleGoodsService iSaleGoodsService;

    @Autowired
    private MateImportMapper mateImportMapper;

    /**
     * 查询采购订单入库信息
     */
    @RequiresPermissions("business:inventoryView:view")
    @GetMapping()
    public String operlog() {
        return prefix + "/inventoryView";
    }

    /**
     * 查询采购订单入库信息列表
     */
    @RequiresPermissions("business:inventoryView:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "beginTime", required = false) String beginTime,
                              @RequestParam(value = "endTime", required = false) String endTime) {
        ProcurementOrder procurementOrder = new ProcurementOrder();
        procurementOrder.setBeginTime(beginTime);
        procurementOrder.setEndTime(endTime);
        startPage();
        procurementOrder.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ProcurementOrder> list = inventoryViewService.selectList(procurementOrder);
        return getDataTable(list);
    }


    @RequiresPermissions("business:inventoryView:list")
    @GetMapping("/matelist")
    @ResponseBody
    public TableDataInfo matelist(@RequestParam(value = "beginTime", required = false) String beginTime,
                                  @RequestParam(value = "endTime", required = false) String endTime) {
        startPage();
        MateImportModel mateImportModel = new MateImportModel();
        mateImportModel.setBeginTime(beginTime);
        mateImportModel.setEndTime(endTime);
        mateImportModel.setDeptId(SecurityUtils.getDeptId());
        List<MateImportModel> list = mateImportMapper.selectModels(mateImportModel);

        return getDataTable(list);
    }


    @GetMapping("/mtedit/{mateProcurementId}")
    @ResponseBody
    public Map<String, Object> mtedit(@PathVariable("mateProcurementId") Long mateProcurementId) {

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> resMap = new HashMap<>();

        MateProcurement mateProcurement = new MateProcurement();
        mateProcurement.setMateProcurementId(mateProcurementId);

        MateProcurement mateProcurement1 = mateProcurementMapper.selectMateProcurementBy(mateProcurement);

        List<MateProcurementModel> mateProcurementModels = mateProcurement1.getMateProcurementModels();

        //List<ProcurementModel> procurementModels = procurementServer.selectProcurementModelById(procurementWarehousingId);
        Map<String, Object> map = new HashMap<>();
        SysUser user1 = userService.selectUserById(mateProcurement1.getUserId());
        Audit audit = new Audit();
        audit.setOrderId(mateProcurementId);
        audit.setOrderType("8");
        Audit audit1 = auditService.selectAuditByOrderId(audit);
        Map<String, Object> mapa = new HashMap<>();

        mapa.put("companyId", SecurityUtils.getLoginUser().getCompanyId());

        MaterialManufactuter materialManufactuter = mateProcurementMapper.selctMateManufactuter(mapa);

        Long companyId = sysUser.getCompanyId();

        resMap.put("companyOhterAddress", materialManufactuter.getMateAddress());
        resMap.put("phone", materialManufactuter.getPhone());

        //  Company company = iCompanyServer.selectByCompanyId(companyId);
        //  resMap.put("address", company.getRasProvince() + company.getRasCity() + company.getRasDistrict() + company.getRasAddress());
        //   Company company1 = iCompanyServer.selectByCompanyId(procurementOrder1.getSupplierId());
        map.put("companyId", companyId);
            /*CompanyOther companyOther = iCompanyOtherServer.selectByCompanyIdAndDeptId(map);
            resMap.put("companyOhter",companyOther);*/


        Company companyInvoice2 = iCompanyServer.selectByCompanyId(sysUser.getCompanyId());
        resMap.put("receiverAddress", companyInvoice2.getRasAddress());
        if (audit1 != null) {
            resMap.put("auditBy", audit1.getAuditBy());
        }
        if (user1 != null) {

            resMap.put("userName", user1.getNickName());
        }
        SysUser user = userService.selectUserByDeptId(sysUser.getCompanyId());
        resMap.put("deptName", materialManufactuter.getMaterialName());
        resMap.put("companyName", user.getNickName());
        resMap.put("procurementOrder1", mateProcurement1);
        resMap.put("orderType", "(原材料入库)");
        return resMap;

    }

    /**
     * 重写分页数据
     *
     * @param msg  提示信息
     * @param list 数据
     * @return 结果
     */
    protected TableDataInfo getDataTable2(String msg, List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(list.size());
        return rspData;
    }

    /**
     * 修改入库信息
     */
    @GetMapping("/edit/{procurementWarehousingId}")
    @ResponseBody
    public Map<String, Object> edit(@PathVariable("procurementWarehousingId") Long procurementWarehousingId) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("user", userService.selectSalesmanList(sysUser.getCompanyId()));
        ProcurementOrder procurementOrders = new ProcurementOrder();
        procurementOrders.setProcurementWarehousingId(procurementWarehousingId);
        ProcurementOrder procurementOrder1 = procurementServer.selectProcurementAndReturn(procurementOrders);
        List<ProcurementModel> procurementModelList = procurementOrder1.getProcurementModelList();
        for (ProcurementModel procurementModel : procurementModelList) {
            if (procurementModel.getUnit().equals("盒") || procurementModel.getUnit() == "盒") {
                Long b = procurementModel.getAllNum() / procurementModel.getFactor();
                procurementModel.setAllNum(procurementModel.getAllNum() / procurementModel.getFactor());
                procurementModel.setWarehousingAmount(b.intValue());
            }
        }

        //List<ProcurementModel> procurementModels = procurementServer.selectProcurementModelById(procurementWarehousingId);
        Map<String, Object> map = new HashMap<>();
        SysUser user1 = userService.selectUserById(procurementOrder1.getUserId());
        Audit audit = new Audit();
        audit.setOrderId(procurementWarehousingId);
        audit.setOrderType("2");
        audit.setCompanyId(SecurityUtils.getDeptId());
        audit.setOrderNum(procurementOrder1.getProcurementWarehousingName());
        Audit audit1 = auditService.selectAuditByOrderId(audit);
        if (procurementOrder1.getSupplierId() != null) {
            CompanyInvoice companyInvoice = companyInvoiceService.selectByCompanyId(procurementOrder1.getSupplierId());
            if (companyInvoice != null) {
                if (StringUtils.isNotEmpty(companyInvoice.getCorporateAddress())) {
                    procurementOrder1.setCompanyAdress(companyInvoice.getCorporateAddress());
                } else {
                    procurementOrder1.setCompanyAdress("");
                }
                if (StringUtils.isNotEmpty(companyInvoice.getCorporateRegisterPhone())) {
                    procurementOrder1.setComPhone(companyInvoice.getCorporateRegisterPhone());
                } else {
                    procurementOrder1.setComPhone("");
                }
            }
            if (procurementOrder1.getSupplierId() != null) {
                map.put("deptId", procurementOrder1.getSupplierId());
                Long companyId = sysUser.getCompanyId();
                map.put("companyId", companyId);
                CompanyOther companyOther = iCompanyOtherServer.selectByCompanyIdAndDeptId(map);
                resMap.put("companyOther", companyOther);
                resMap.put("supplier", userService.selectSupplierById(sysUser.getCompanyId()));
                map.put("deptId", procurementOrder1.getSupplierId());
                resMap.put("deptId", procurementOrder1.getSupplierId());
            } else {
                resMap.put("supplier", "");
            }
            Long companyId = sysUser.getCompanyId();
            Company company = iCompanyServer.selectByCompanyId(companyId);
            resMap.put("address", company.getRasProvince() + company.getRasCity() + company.getRasDistrict() + company.getRasAddress());
            Company company1 = iCompanyServer.selectByCompanyId(procurementOrder1.getSupplierId());
            resMap.put("companyOhterAddress", company1.getRasProvince() + company1.getRasCity() + company1.getRasDistrict() + company1.getRasAddress());
            resMap.put("phone", company1.getCorporateRegisterPhone());
            map.put("companyId", companyId);
            /*CompanyOther companyOther = iCompanyOtherServer.selectByCompanyIdAndDeptId(map);
            resMap.put("companyOhter",companyOther);*/
            resMap.put("supplier", userService.selectSupplierById(sysUser.getCompanyId()));
        }
        //   CompanyInvoice companyInvoice2 = companyInvoiceService.selectByCompanyId(sysUser.getCompanyId());
        // resMap.put("receiverAddress",companyInvoice2.getCorporateAddress());
        if (audit1 != null) {
            resMap.put("auditBy", audit1.getAuditBy());
        }
        if (user1 != null) {

            resMap.put("userName", user1.getNickName());
        }
        SysUser user = userService.selectUserByDeptId(sysUser.getCompanyId());
        resMap.put("deptName", procurementOrder1.getDeptName());
        resMap.put("companyName", user.getNickName());
        resMap.put("procurementOrder1", procurementOrder1);
        resMap.put("orderType", "(采购入库)");
        return resMap;

    }

}
